package com.tianma315.admin.qa.controller;


import java.util.Arrays;
import java.util.Date;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.qa.vo.QaPatrolVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.qa.domain.QaPatrolDO;
import com.tianma315.admin.qa.service.QaPatrolService;

/**
 * 
 * <pre>
 * 品控巡查
 * </pre>
 * <small> 2019-07-10 09:48:55 | wen</small>
 */
@Controller
@RequestMapping("/qa/qaPatrol")
public class QaPatrolController extends BaseController{
	@Autowired
	private QaPatrolService qaPatrolService;
	
	@GetMapping()
	@RequiresPermissions("qa:qaPatrol:qaPatrol")
	String QaPatrol(){
	    return "qa/qaPatrol/qaPatrol";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("qa:qaPatrol:qaPatrol")
	public Result<Page<QaPatrolVO>> list(Integer pageNumber, Integer pageSize, QaPatrolVO qaPatrolDTO,Date beginDate,
										 Date endDate){
		// 查询列表数据
        Page<QaPatrolVO> page = qaPatrolService.getPatroVOPage(pageNumber, pageSize,qaPatrolDTO,beginDate,endDate);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("qa:qaPatrol:add")
	String add(){
	    return "qa/qaPatrol/add";
	}

	@GetMapping("/edit/{qaPatrolId}")
	@RequiresPermissions("qa:qaPatrol:edit")
	String edit(@PathVariable("qaPatrolId") Integer qaPatrolId,Model model){
		QaPatrolVO qaPatrol = qaPatrolService.getQaPatrolById(qaPatrolId);
		model.addAttribute("qaPatrol", qaPatrol);
	    return "qa/qaPatrol/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("qa:qaPatrol:add")
	public Result<String> save( QaPatrolVO qaPatrol){
		qaPatrol.setCheckBy(getUserId());
		qaPatrol.setCreateBy(getUserId());
		qaPatrol.setState(0);
		qaPatrol.setCreateDate(new Date());
		if(qaPatrolService.addQaPatrol(qaPatrol)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("qa:qaPatrol:edit")
	public Result<String>  update( QaPatrolVO qaPatrol){
//		System.out.println(qaPatrol.getPatrolDate());
		qaPatrolService.updateQaPatrol(qaPatrol);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("qa:qaPatrol:remove")
	public Result<String>  remove( Integer qaPatrolId){
		if(qaPatrolService.deleteQaPatrol(qaPatrolId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("qa:qaPatrol:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] qaPatrolIds){
//		qaPatrolService.deleteBatchIds(Arrays.asList(qaPatrolIds));
		for (Integer id:qaPatrolIds) {
			qaPatrolService.deleteQaPatrol(id);
		}
		return Result.ok();
	}
	
}
